<template>
  <div class="device_safe">
    <one-title title="设备安全记录" @refreshData="refresh"></one-title>
    <div class="fixed_top_56">
      <mes-search-comp
        ref="mesSearch"
        @searchInfo="_info"
        :searchList="tableColConfig"
        :searchResult="searchResult">
      </mes-search-comp>
      <div class="table_container">
        <el-table
          ref="mesTable"
          border
          stripe
          v-loading="isLoading"
          style="width: 100%"
          :data="listData.list"
          :max-height="tableMaxHeight"
          @sort-change="_sort">
          <mes-col-comp
            v-for="(column,index) in columnList"
            :key="index"
            :column="column">
          </mes-col-comp>
          <el-table-column
            label="操作"
            :resizable="false"
            align="center"
            fixed="right"
            min-width="100"
            show-overflow-tooltip>
            <template slot-scope="scope">
            <span class="jump_item"
                  :class="{is_disabled: !scope.row.checkRecord}"
                  @click="showDeviceRecord(scope.row.checkRecord)">查看记录</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <mes-pagination
        @updateData="init"
        :currentPage="listData.pageNo"
        :total="listData.count || 0"
        :pageSize="listData.pageSize">
      </mes-pagination>
    </div>
    <el-dialog
      title="设备检查记录"
      :visible.sync="dialogVisible"
      width="800"
      v-dialogDrag
      :close-on-click-modal="false"
      customClass="mes_dialog">
      <equipment-template ref="EquipmentTemplate" :readonly="true"></equipment-template>
      <span slot="footer" class="dialog-footer mes_btn">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import EquipmentTemplate from '@/components/Template/EquipmentTemplate'
import tableColumn from './tableColumn'

export default {
  name: 'DeviceSafe',
  data() {
    return {
      tableColConfig: tableColumn, //列配置
      dialogVisible: false, // 是否显示
      isLoading: false, // 加载中
      searchData: {}, // 搜索参数
      sortData: {}, // 排序参数
      searchResult: NaN, // 搜索结果数
    }
  },
  components: {
    EquipmentTemplate
  },
  computed: {
    // 获取列表数据
    listData() {
      return this.$store.state.deviceSafe.listData
    },
    // 列循环
    columnList() {
      return this.tableColConfig.filter(item => item.noForCol !== true && item.isShow !== false)
    },
  },
  mounted() {
    this.init()
  },
  methods: {
    // 刷新
    refresh() {
      this.$refs.mesSearch.clean()
      this.$refs.mesTable.clearSort()
      this.searchData = {}
      this.sortData = {}
      this.init()
    },
    // 初始化
    init(page) {
      this.isLoading = true
      let postData = {
        method: 'post',
        url: 'a/devicecheckhistory/deviceCheckHistory/list',
        data: {
          pageNo: page ? page.pageNo : 1,
          pageSize: page ? page.pageSize : this.listData.pageSize || 15,
          query: this.searchData,
          sort: this.sortData,
        },
        module: 'deviceSafe',
        state: 'listData'
      }
      this.$store.dispatch('saveData', postData).then((res) => {
        if (Object.keys(this.searchData).length > 0) {
          this.searchResult = res.count
        } else {
          this.searchResult = NaN
        }
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 打开检查记录弹框
    showDeviceRecord(record) {
      if (record) {
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs.EquipmentTemplate.openDialog(JSON.parse(record))
        })
      }
    },
    // 查询
    _info(data) {
      this.searchData = data
      this.init()
    },
    // 排序
    _sort(item) {
      this.sortData = this.$getSortOrder(item)
      this.init()
    },
  }
}
</script>

<style scoped lang="less">
</style>
